<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="部门名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.departmentName"
                placeholder="请输入部门名称"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5" v-hasPromise="'departmentDel'">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="状态"
            >
              <a-select
                style="width: 100%"
                v-model.trim="queryParam.deleteFlag"
              >
                <a-select-option
                  v-for="item in deleteOption"
                  :key="item.value"
                  :value="item.value"
                >
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col :span="22">
          <a-button type="primary" @click="handleAdd">新建</a-button>
          <!-- <a-button type="primary" @click="handleAdd">导出</a-button> -->
          <!-- <a-button type="primary" @click="handleAdd">导入</a-button> -->
        </a-col>
        <a-col :span="2">
          <a-button type="primary" @click="handleAllList">全部部门</a-button>
        </a-col>
      </a-row>
      <br />
      <a-row :gutter="16">
        <a-col :span="5">
          <div class="treeBox">
            <a-tree
              :show-line="true"
              :checkable="false"
              :tree-data="treeData"
              v-if="treeData.length > 0"
              :selectedKeys="selectedKeys"
              defaultExpandAll
              :replaceFields="{
                children: 'childList',
                title: 'departmentName',
                key: 'id',
                value: 'id',
              }"
            >
            </a-tree>
          </div>
        </a-col>
        <a-col :span="19">
          <a-table
            :columns="columns"
            :data-source="tableData"
            :loading="loading"
            :pagination="pagination"
            @change="handleTableChange"
            bordered
            :rowKey="
              (record, index) => {
                return index;
              }
            "
          >
            <template slot="action" slot-scope="text, record">
              <div v-if="record.deleteFlag === 'N'">
                <a @click="handleEdit(record)">编辑</a>
                <a-divider type="vertical" v-hasPromise="'departmentDel'" />
                <a-popconfirm
                  okText="确定"
                  cancelText="取消"
                  v-hasPromise="'departmentDel'"
                  title="确定删除吗?"
                  @confirm="() => handleDelete(record)"
                >
                  <a>删除</a>
                </a-popconfirm>
              </div>
              <a-popconfirm
                v-else
                okText="确定"
                cancelText="取消"
                title="确定恢复吗?"
                @confirm="() => handleRecovery(record)"
              >
                <a>恢复</a>
              </a-popconfirm>
            </template>
          </a-table>
        </a-col>
      </a-row>
    </a-card>

    <DepartmentModal
      ref="modalForm"
      :treeData="treeData"
      @ok="modalFormOk"
      @getTree="getTree"
    ></DepartmentModal>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import DepartmentModal from "@/components/DepartmentModal.vue";
import api from "@/api/apiServer";
export default {
  components: { DepartmentModal },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      treeData: [],
    };
  },
  mounted() {
    this.getTree();
    this.getList();
  },
  methods: {
    // 获取列表数据
    async getList(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.DepartmentList(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
    },
    async getTree() {
      const res = await api.DepartmentTree();
      this.treeData = res.data;
    },
    // 删除
    async handleDelete(record) {
      let params = {
        userName: record.userName,
        id: record.id,
      };
      const res = await api.DepartmentDelete(params);
      this.$message.success(res.message);
      this.getList();
      this.getTree();
    },
    // 恢复
    async handleRecovery(record) {
      let params = {
        id: record.id,
      };
      const res = await api.DepartmentRecovery(params);
      this.$message.success(res.message);
      this.getList();
      this.getTree();
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
